import 'dart:io';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:study_online/entity/CourseRou.dart';
import 'package:study_online/entity/Student.dart';
import 'package:study_online/global/colors.dart';

import 'package:flutter/material.dart';
import 'package:study_online/global/global.dart';
import 'package:study_online/global/myIcons.dart';
import 'package:study_online/routes/CustomRoute.dart';

import 'UserInformationPage.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        //appBar: AppBar(
        //  backgroundColor: Colors.black26,
        //  title: Text('container'),
        //),
        body: MyPage(),
      ),
    );
  }
}

class MyPage extends StatefulWidget {
  MyPage({Key key}) : super(key: key);

  @override
  _MyPageState createState() {
    return _MyPageState();
  }
}

class _MyPageState extends State<MyPage> with AutomaticKeepAliveClientMixin{
  Student userInfo;
  int _num=0;
  @override
  void initState(){
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    //debugPaintSizeEnabled = true;
    userInfo = Global.getInstance().user;
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(313, 557),
    );
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        backgroundColor: Color(0xffA1FFA2),
        leading: Icon(
          MyIcons.scan,
          color: Colors.black,
        ),
        title: Text(
          '个人中心',
          style: TextStyle(
            fontSize: ScreenUtil().setSp(17),
            color: Colors.black,
          ),
          textAlign: TextAlign.center,
        ),
        actions: [
          InkWell(
            onTap: (){
              CourseRou _cou=new CourseRou(1, 'https://img1.baidu.com/it/u=2832876687,349962718&fm=26&fmt=auto', '女老师', 'couName');
              Navigator.pushNamed(context, 'courseStudy',arguments:[_cou] );
            },
            child: Icon(
              MyIcons.setting,
              color: Colors.black,
            ),
          ),
          SizedBox(
            width: ScreenUtil().setWidth(8),
          ),
        ],
      ),
      backgroundColor: AppColors.background,
      body: MediaQuery.removePadding(
        context: context,
        removeTop: true,
        child: Stack(
          children: [
            Container(
              height: double.infinity,
              width: double.infinity,
            ),
            //背景
            Positioned(
              top: 0,
              left: 0,
              child: Image.asset(
                'assets/img/head_img_bg1.jpg',
                width: ScreenUtil().screenWidth,
                height: ScreenUtil().setHeight(134),
                fit: BoxFit.cover,
              ),
            ),
            //头像
            Positioned(
              top: 0,
              left: 0,
              child: Column(
                children: AnimationConfiguration.toStaggeredList(
                  duration: const Duration(milliseconds: 475),
                  childAnimationBuilder: (widget) => SlideAnimation(
                    horizontalOffset: MediaQuery.of(context).size.width / 2,
                    child: FadeInAnimation(child: widget),
                  ),
                  children: [
                    Container(
                      width: ScreenUtil().screenWidth -
                          2.0 * ScreenUtil().setHeight(10),
                      height: ScreenUtil().setHeight(45),
                      margin: EdgeInsets.fromLTRB(
                          ScreenUtil().setHeight(10),
                          ScreenUtil().setWidth(6),
                          ScreenUtil().setWidth(10),
                          0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          ClipOval(
                            child: Image.network(
                                userInfo == null
                                    ? "https://img2.baidu.com/it/u=172917783,2425047487&fm=26&fmt=auto"
                                    : userInfo.photo,
                                width: ScreenUtil().setSp(45),
                                height: ScreenUtil().setSp(45),
                                fit: BoxFit.cover),
                          ),
                          Container(
                            height: ScreenUtil().setHeight(45),
                            margin: EdgeInsets.only(
                                left: ScreenUtil().setWidth(10)),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  userInfo != null
                                      ? userInfo.name
                                      : '雨后qingtian',
                                  style: TextStyle(
                                      fontSize: ScreenUtil().setSp(20),
                                      color: Colors.black,
                                      fontFamily: '方正御墨行书'),
                                ),
                                Text(
                                  '湖南中医药大学 | 信科院 | ' +
                                      (userInfo == null
                                          ? '计科1班'
                                          : userInfo.major),
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(13),
                                    color: Colors.black,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      width: ScreenUtil().setWidth(270),
                      margin: EdgeInsets.only(top: ScreenUtil().setHeight(13)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        //crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            height: ScreenUtil().setHeight(42),
                            width: ScreenUtil().setWidth(40),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Text(
                                  (Random().nextInt(10) + 10).toString(),
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(20),
                                    color: Colors.black,
                                  ),
                                ),
                                Text(
                                  '课程收藏',
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(9),
                                    color: Colors.black,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          InkWell(
                            onTap: (){
                              Navigator.pushNamed(context, 'contactsPage');
                            },
                            child: Container(
                              height: ScreenUtil().setHeight(42),
                              width: ScreenUtil().setWidth(40),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Text(
                                    "${Global.getInstance().num}",
                                    style: TextStyle(
                                      fontSize: ScreenUtil().setSp(20),
                                      color: Colors.black,
                                    ),
                                  ),
                                  Text(
                                    '好友数量',
                                    style: TextStyle(
                                      fontSize: ScreenUtil().setSp(9),
                                      color: Colors.black,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Container(
                            height: ScreenUtil().setHeight(42),
                            width: ScreenUtil().setWidth(40),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Text(
                                  (Random().nextInt(10) + 10).toString(),
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(20),
                                    color: Colors.black,
                                  ),
                                ),
                                Text(
                                  '学习时长',
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(9),
                                    color: Colors.black,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          Container(
                            height: ScreenUtil().setHeight(42),
                            width: ScreenUtil().setWidth(40),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Text(
                                  (Random().nextInt(10) + 10).toString(),
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(20),
                                    color: Colors.black,
                                  ),
                                ),
                                Text(
                                  '学习排名',
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(9),
                                    color: Colors.black,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(
                          ScreenUtil().setWidth(10),
                          ScreenUtil().setHeight(8),
                          ScreenUtil().setWidth(10),
                          0),
                      width: ScreenUtil().setWidth(290),
                      height: ScreenUtil().setHeight(40),
                      child: Stack(
                        children: [
                          Container(
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(8),
                              child: Image.asset(
                                'assets/img/vip_bg.png',
                                width: ScreenUtil().setWidth(290),
                                height: ScreenUtil().setHeight(40),
                                fit: BoxFit.cover,
                              ),
                            ),
                            // decoration: BoxDecoration(
                            //   shape: BoxShape.rectangle,
                            //  borderRadius: BorderRadius.all(Radius.circular(10)),
                            // ),
                          ),
                          Positioned(
                            top: ScreenUtil().setHeight(5),
                            left: 0,
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                SizedBox(
                                  width: ScreenUtil().setWidth(12),
                                ),
                                Image.asset(
                                  'assets/img/vip.png',
                                  width: ScreenUtil().setWidth(28),
                                  height: ScreenUtil().setHeight(23),
                                  fit: BoxFit.fitWidth,
                                ),
                                SizedBox(
                                  width: ScreenUtil().setWidth(10),
                                ),
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      '会员权益卡',
                                      style: TextStyle(
                                          fontSize: ScreenUtil().setSp(14),
                                          color: Colors.white,
                                          fontWeight: FontWeight.bold),
                                    ),
                                    SizedBox(
                                      width: ScreenUtil().setHeight(6),
                                    ),
                                    Text(
                                      '课程5折起',
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(8),
                                        color: Color(0xffBD904D),
                                      ),
                                    ),
                                  ],
                                ),
                                SizedBox(
                                  width: ScreenUtil().setWidth(78),
                                ),
                                InkWell(
                                  child: Container(
                                    margin: EdgeInsets.only(
                                        top: ScreenUtil().setHeight(1)),
                                    width: ScreenUtil().setWidth(74),
                                    height: ScreenUtil().setHeight(20),
                                    decoration: BoxDecoration(
                                      boxShadow: [
                                        BoxShadow(
                                            color: Colors.grey,
                                            offset: Offset(1.0, 1.0),
                                            blurRadius: 2.0)
                                      ],
                                      color: AppColors.primary,
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(
                                              ScreenUtil().setHeight(16))),
                                    ),
                                    alignment: Alignment.center,
                                    child: Text(
                                      '开通会员',
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.white,
                                      ),
                                    ),
                                  ),
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(
                          ScreenUtil().setWidth(10),
                          ScreenUtil().setHeight(9),
                          ScreenUtil().setWidth(10),
                          0),
                      height: ScreenUtil().setHeight(89),
                      width: ScreenUtil().screenWidth -
                          2 * ScreenUtil().setWidth(10),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            width: ScreenUtil().setWidth(138),
                            height: ScreenUtil().setHeight(86),
                            padding: EdgeInsets.only(
                                left: ScreenUtil().setWidth(15)),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10)),
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.black26,
                                  offset: Offset(-1.5, 1.5),
                                  blurRadius: 1.5,
                                  spreadRadius: 0.6,
                                ),
                              ],
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(height: ScreenUtil().setHeight(12)),
                                Text(
                                  '个人信息',
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(15),
                                    color: Colors.black,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                                SizedBox(height: ScreenUtil().setHeight(6)),
                                Text(
                                  '信息已完善87.5%',
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(11),
                                    color: Colors.grey,
                                  ),
                                ),
                                SizedBox(height: ScreenUtil().setHeight(6)),
                                InkWell(
                                  onTap: (){
                                    Navigator.of(context).push(CustomRoute(UserInformationPage()));
                                  },
                                  child: Container(
                                    margin: EdgeInsets.only(
                                        top: ScreenUtil().setHeight(1)),
                                    width: ScreenUtil().setWidth(63),
                                    height: ScreenUtil().setHeight(17),
                                    decoration: BoxDecoration(
                                      color: AppColors.primary,
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(
                                              ScreenUtil().setHeight(16))),
                                    ),
                                    alignment: Alignment.center,
                                    child: Text(
                                      '完善信息',
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(10),
                                        color: Colors.white,
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                          Container(
                            width: ScreenUtil().setWidth(138),
                            height: ScreenUtil().setHeight(86),
                            padding: EdgeInsets.only(
                                left: ScreenUtil().setWidth(15)),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10)),
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.black26,
                                  offset: Offset(-1.5, 1.5),
                                  blurRadius: 1.5,
                                  spreadRadius: 0.6,
                                ),
                              ],
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(height: ScreenUtil().setHeight(12)),
                                Text(
                                  '我的钱包',
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(15),
                                    color: Colors.black,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                                SizedBox(height: ScreenUtil().setHeight(15)),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Column(
                                      children: [
                                        Row(
                                          children: [
                                            Text(
                                              '888',
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(15),
                                                color: AppColors.primary,
                                              ),
                                            ),
                                            Text(
                                              '分',
                                              style: TextStyle(
                                                fontSize: ScreenUtil().setSp(8),
                                                color: Colors.black,
                                              ),
                                            ),
                                          ],
                                        ),
                                        Text(
                                          '积分签到 >',
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(10),
                                            color: Colors.grey,
                                          ),
                                        ),
                                      ],
                                    ),
                                    SizedBox(
                                      width: ScreenUtil().setWidth(20),
                                    ),
                                    Column(
                                      children: [
                                        Row(
                                          children: [
                                            Text(
                                              userInfo == null
                                                  ? '166.6'
                                                  : userInfo.money.toString(),
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(15),
                                                color: AppColors.primary,
                                              ),
                                            ),
                                            Text(
                                              '元',
                                              style: TextStyle(
                                                fontSize: ScreenUtil().setSp(8),
                                                color: Colors.black,
                                              ),
                                            ),
                                          ],
                                        ),
                                        Text(
                                          '个人钱包 >',
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(10),
                                            color: Colors.grey,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      height: ScreenUtil().setHeight(165),
                      width: ScreenUtil().screenWidth -
                          2.0 * ScreenUtil().setWidth(10),
                      margin: EdgeInsets.fromLTRB(
                          ScreenUtil().setWidth(10),
                          ScreenUtil().setHeight(8),
                          ScreenUtil().setWidth(10),
                          0),
                      padding: EdgeInsets.only(top: ScreenUtil().setHeight(6)),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              SizedBox(
                                width: ScreenUtil().setWidth(10),
                              ),
                              Text(
                                '常用功能',
                                style: TextStyle(
                                    fontSize: ScreenUtil().setSp(16),
                                    color: Colors.black,
                                    fontWeight: FontWeight.bold),
                              ),
                            ],
                          ),
                          Divider(
                            color: Colors.grey,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              Container(
                                width: ScreenUtil().setWidth(43),
                                height: ScreenUtil().setHeight(50),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Image.asset(
                                      'assets/img/download.png',
                                      width: ScreenUtil().setSp(41),
                                      height: ScreenUtil().setSp(41),
                                      fit: BoxFit.fitHeight,
                                    ),
                                    Text(
                                      '离线缓存',
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(8),
                                        color: Colors.grey,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Container(
                                width: ScreenUtil().setWidth(43),
                                height: ScreenUtil().setHeight(50),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Image.asset(
                                      'assets/img/exercise.png',
                                      width: ScreenUtil().setSp(41),
                                      height: ScreenUtil().setSp(41),
                                      fit: BoxFit.fitHeight,
                                    ),
                                    Text(
                                      '我的练习',
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(8),
                                        color: Colors.grey,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Container(
                                width: ScreenUtil().setWidth(43),
                                height: ScreenUtil().setHeight(50),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Image.asset(
                                      'assets/img/customer_service.png',
                                      width: ScreenUtil().setSp(41),
                                      height: ScreenUtil().setSp(41),
                                      fit: BoxFit.fitHeight,
                                    ),
                                    Text(
                                      '联系客服',
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(8),
                                        color: Colors.grey,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Container(
                                width: ScreenUtil().setWidth(43),
                                height: ScreenUtil().setHeight(50),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Image.asset(
                                      'assets/img/backfeed.png',
                                      width: ScreenUtil().setSp(41),
                                      height: ScreenUtil().setSp(41),
                                      fit: BoxFit.fitHeight,
                                    ),
                                    Text(
                                      '帮助与反馈',
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(8),
                                        color: Colors.grey,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                          Container(
                            width: ScreenUtil().setWidth(43),
                            height: ScreenUtil().setHeight(50),
                            margin: EdgeInsets.only(
                                top: ScreenUtil().setHeight(10),
                                left: ScreenUtil().setWidth(17)),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset(
                                  'assets/img/share.png',
                                  width: ScreenUtil().setSp(41),
                                  height: ScreenUtil().setSp(41),
                                  fit: BoxFit.fitHeight,
                                ),
                                Text(
                                  '分享APP',
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(8),
                                    color: Colors.grey,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                      decoration: BoxDecoration(
                        boxShadow: [
                          BoxShadow(
                            color: Colors.black26,
                            offset: Offset(-1.5, 1.5),
                            blurRadius: 1.5,
                            spreadRadius: 0.6,
                          ),
                        ],
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive =>true;
}
